package com.studio77.guessLyric.layer;

import com.studio77.guessLyric.R;
import com.studio77.guessLyric.core.GameSystem;
import com.studio77.guessLyric.scene.MainScene;
import com.studio77.guessLyric.util.GameUtil;
import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.Animate;
import com.wiyun.engine.actions.CallFunc;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.nodes.Animation;
import com.wiyun.engine.nodes.ColorLayer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYColor4B;
import com.wiyun.engine.types.WYPoint;

public class LoadingLayer extends ColorLayer {
	public LoadingLayer() {
		super(WYColor4B.make(255, 255, 255, 255));
		// 显示loading动画
		Animation animation = new Animation(0, 0.3f, R.drawable.loading_0,
				R.drawable.loading_1, R.drawable.loading_2,
				R.drawable.loading_3, R.drawable.loading_4,
				R.drawable.loading_5, R.drawable.loading_6);
		animation.autoRelease();
		Animate animate = Animate.make(animation);
		animate.autoRelease();

		Sprite sprite = Sprite.make(R.drawable.loading_0);
		addChild(sprite);
		sprite.setPosition(GameSystem.Screen_Width / 2,
				GameSystem.Screen_Height / 2 + 70);
		sprite.autoRelease();
		Sprite logo = Sprite.make(R.drawable.logo77);
		logo.autoRelease();
		logo.setPosition(GameSystem.Screen_Width / 2,
				GameSystem.Screen_Height / 2 + 120);
		addChild(logo);

		sprite.runAction((Action) RepeatForever.make(animate).autoRelease());
		sprite.runAction((CallFunc) CallFunc.make(this, "loadResource")
				.autoRelease());

		Sprite wilogo = Sprite
				.make(Texture2D.makePNG(R.drawable.wiengine_logo));
		addChild(wilogo);
		wilogo.setAnchor(1, 0);
		wilogo.setPosition(WYPoint.make(GameSystem.Screen_Width - 5, 5));
		wilogo.autoRelease();
	}

	/**
	 * 加载系统资源
	 */
	public void loadResource() {
		new Thread() {
			public void run() {
				GameSystem.loadAllResource();
				gotoGameMenu();
			}
		}.start();
	}

	/**
	 * 切换到游戏菜单s
	 */
	public void gotoGameMenu() {
		GameUtil.switchSceneNormal(MainScene.make());
	}
}
